module TopModule(
    input clk_100MHz,
    input rst_n,
    input [8:0] key_input_n,      // 按键低电平有效

    output [7:0] segs,
    output [3:0] an,
    output [3:0] bcd_penalty_high,
    output [3:0] bcd_penalty_low
);

    // 时钟分频：只分出10Hz，1kHz等用不到可以忽略
    wire clk_10Hz;

    ClockDivider clkdiv(
        .clk_100MHz(clk_100MHz),
        .rst_n(rst_n),
        .clk_1kHz(),    // 既然不用，就不连接
        .clk_10Hz(clk_10Hz)
    );

    // 按键消抖模块（用100MHz时钟）
    wire key_ready;
    wire [3:0] key_num;
    KeyScanDebounce keyscan(
        .clk(clk_100MHz),
        .rst_n(rst_n),
        .key_in_n(key_input_n),
        .key_ready(key_ready),
        .key_num(key_num)
    );

    // 随机数字生成器（用100MHz时钟）
    wire [3:0] current_num;
    wire rnd_done;
    reg rnd_start;
    reg rnd_next;

    RandomNumberGen rndgen(
        .clk(clk_100MHz),
        .rst_n(rst_n),
        .start(rnd_start),
        .next(rnd_next),
        .num(current_num),
        .done(rnd_done)
    );

    // 计时与惩罚模块（用10Hz时钟）
    wire [11:0] time_used;
    wire [7:0] time_penalty;
    reg key_correct;
    reg key_wrong;
    reg game_running;
    reg game_done;

    TimerPenaltyLogic timer(
        .clk_10Hz(clk_10Hz),
        .rst_n(rst_n),
        .start(game_running),
        .key_correct(key_correct),
        .key_wrong(key_wrong),
        .game_done(game_done),
        .time_used(time_used),
        .time_penalty(time_penalty),
        .game_running()
    );

    // 状态机：在100MHz时钟下运行
    localparam IDLE = 3'd0,
               START = 3'd1,
               WAIT_KEY = 3'd2,
               CHECK = 3'd3,
               NEXT = 3'd4,
               FINISH = 3'd5;

    reg [2:0] state = IDLE;
    reg key_used;

    always @(posedge clk_100MHz or negedge rst_n) begin
        if (!rst_n) begin
            state <= IDLE;
            rnd_start <= 0;
            rnd_next <= 0;
            game_running <= 0;
            game_done <= 0;
            key_correct <= 0;
            key_wrong <= 0;
            key_used <= 0;
        end else begin
            // 复位或默认时清除控制信号
            rnd_start <= 0;
            rnd_next <= 0;
            key_correct <= 0;
            key_wrong <= 0;

            case(state)
                IDLE: begin
                    rnd_start <= 1;  // 复位后启动随机数生成
                    game_done <= 0;
                    game_running <= 0;
                    key_used <= 0;
                    state <= START;
                end
                START: begin
                    if (rnd_done) begin
                        game_running <= 1;
                        state <= WAIT_KEY;
                    end else begin
                        rnd_start <= 1;  // 持续启动直到完成
                    end
                end
                WAIT_KEY: begin
                    if (key_ready && !key_used) begin
                        state <= CHECK;
                    end
                end
                CHECK: begin
                    if (key_num == current_num) begin
                        key_correct <= 1;
                        key_used <= 1;
                        if (rnd_done) begin
                            game_done <= 1;
                            game_running <= 0;
                            state <= FINISH;
                        end else begin
                            state <= NEXT;
                        end
                    end else begin
                        key_wrong <= 1;
                        key_used <= 1;
                        state <= WAIT_KEY;
                    end
                end
                NEXT: begin
                    rnd_next <= 1;
                    key_used <= 0;
                    state <= WAIT_KEY;
                end
                FINISH: begin
                    // 结束自动重启
                    rnd_start <= 1;
                    game_done <= 0;
                    game_running <= 1;
                    key_used <= 0;
                    state <= START;
                end
                default: state <= IDLE;
            endcase

            if (!key_ready)
                key_used <= 0;
        end
    end

    // 显示模块连接
    DisplayController display_inst(
        .clk(clk_100MHz),
        .rst_n(rst_n),
        .current_num(current_num),
        .time_used(time_used),
        .time_penalty(time_penalty),
        .segs(segs),
        .an(an),
        .bcd_penalty_high(bcd_penalty_high),
        .bcd_penalty_low(bcd_penalty_low)
    );

endmodule
